from NeuralNetwork import *

input_nodes = 784
hidden_nodes = 100
output_nodes = 10
learning_rate = 0.1

if __name__ == '__main__':
    n = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
    training_data_file = open("mnist_train.csv", 'r')
    training_data_list = training_data_file.readlines()
    training_data_file.close()

    epochs = 5
    total_progress = 5 * len(training_data_list)
    current_progress = 0
    for e in range(epochs):
        for record in training_data_list:
            all_values = record.split(',')
            inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
            targets = np.zeros(output_nodes) + 0.01
            targets[int(all_values[0])] = 0.99
            n.train(inputs, targets)
            current_progress += 1
            print("\rtraining progress: {:3}%".format((current_progress / total_progress) * 100), end="")
            pass
        pass

    test_data_file = open("mnist_test.csv", 'r')
    test_data_list = test_data_file.readlines()
    test_data_file.close()

    scorecard = []
    for record in test_data_list:
        all_values = record.split(',')
        correct_label = int(all_values[0])
        inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
        outputs = n.query(inputs)
        label = np.argmax(outputs)
        if (label == correct_label):
            scorecard.append(1)
        else:
            scorecard.append(0)
        pass

    scorecard_array = np.asarray(scorecard)
    print ("\nperformance = ", scorecard_array.sum() / scorecard_array.size)